﻿<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:cs="urn:cs">


  <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
        public int SortRating(int iAll, int iSuccess)
        {
            if(iAll == 0)
            {
                return -1;
            }

            if(iSuccess == 0)
            {
                return 0;
            }

            double dRet1 = 100 * iSuccess / iAll;
            return (int)Math.Round(dRet1);
        }
        
        
        public string CalcRating(int iAll, int iSuccess)
        {
            if(iAll == 0)
            {
                return "-";
            }

            if(iSuccess == 0)
            {
                return "0 %";
            }

            double dRet1 = 100 * iSuccess / iAll;
            double dRet2 = Math.Round(dRet1);

            return dRet2.ToString() + " %";
        }
        
       public string GetTitle(string sUri)
       {
          return sUri.Substring(sUri.LastIndexOf("/") + 1);
       }        
        
     ]]>
  </msxsl:script>

  <xsl:param name="showtopicname" />
  <xsl:param name="topicfilter" />
  <xsl:template match="/">
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <style type="text/css">
          h1 {
          color: green;
          font-family: Tahoma;
          font-size:16px;
          }

          table {
          font-family: Tahoma;
          font-size:12px;}

          td {
          padding-left:15px;
          padding-right:15px;
          padding-top:4px;
          padding-bottom:4px;
          }

          #left{
          float:left;
          width:40%;
          overflow:hidden;
          }

          #right{
          float:left;
          width:60%;
          overflow:hidden;
          }

          a {
          color: black;
          }
        </style>
        <title>Ratings Summary</title>
      </head>
      <body>
        <xsl:call-template name="ReportData">
        </xsl:call-template>
      </body>
    </html>
  </xsl:template>
  <!--=============================================================================================================================================================-->
  <!-- Template to display all data                                                                                                                            -->
  <!--=============================================================================================================================================================-->
  <xsl:template name="ReportData">

    <H1>
      <xsl:text>Topics Summary (30 days): </xsl:text>
      <xsl:value-of select="sum(//Rollups/RecordCount)" />
    </H1>

    <table>
      <tr>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          No
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Title
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Count
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Rating
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Ratings All
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Helpful
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Total Page View
        </th>
        <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
          Distinct Visitors
        </th>

        <xsl:if test="$showtopicname">
          <th noWrap="" style="BORDER-TOP: ghostwhite 1px solid; HEIGHT: 20px; BORDER-BOTTOM: dimgray 1px solid; PADDING-BOTTOM: 5px; TEXT-ALIGN: center; PADDING-TOP: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; BACKGROUND-COLOR: gainsboro">
            Topic Name
          </th>
        </xsl:if>
      </tr>
      <xsl:choose>
        <xsl:when test="not($topicfilter)">
          <xsl:call-template name="ShowAllRows"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:call-template name="ShowFilteredRows"/>
        </xsl:otherwise>
      </xsl:choose>
    </table>
  </xsl:template>
  <!--=============================================================================================================================================================-->
  <!-- Template to display all rows                                                                                                                          -->
  <!--=============================================================================================================================================================-->
  <xsl:template name="ShowAllRows">
    <xsl:for-each select="//Rollups">
      <xsl:sort select="cs:SortRating(RatingCount, HelpfulCount)" data-type="number" order="descending" />
      <xsl:sort select="Title"/>
      <xsl:call-template name="TopicRows"/>
    </xsl:for-each>
  </xsl:template>
  <!--=============================================================================================================================================================-->
  <!-- Template to display filtered rows                                                                                                                          -->
  <!--=============================================================================================================================================================-->
  <xsl:template name="ShowFilteredRows">
    <xsl:for-each select="//Rollups[starts-with(TopicName, $topicfilter)]">
      <xsl:sort select="cs:SortRating(RatingCount, HelpfulCount)" data-type="number" order="descending" />
      <xsl:sort select="Title"/>
      <xsl:call-template name="TopicRows"/>
    </xsl:for-each>

  </xsl:template>
  <!--=============================================================================================================================================================-->
  <!-- Template to display the topic rows                                                                                                                          -->
  <!--=============================================================================================================================================================-->
  <xsl:template name="TopicRows">
    <tr>
      <xsl:if test="(position() mod 2 = 0)">
        <xsl:attribute name="bgcolor">#EEEEFF</xsl:attribute>
      </xsl:if>

      <xsl:if test="hit1 != 1 or hit2 != 1">
        <xsl:attribute name="bgcolor">#FFCC99</xsl:attribute>
      </xsl:if>

      <td style="text-align: center;">
        <xsl:value-of select="position()" />
      </td>


      <td style="text-align: left;">
        <a>
          <xsl:attribute name="href">
            <xsl:value-of select="LiveUrl" />
          </xsl:attribute>
          <xsl:attribute name="target">
            <xsl:text disable-output-escaping="yes">_blank</xsl:text>
          </xsl:attribute>
          <xsl:value-of select=" substring-before(Title, '|')" />
        </a>
      </td>

      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="RecordCount" />
      </td>

      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="cs:CalcRating(RatingCount, HelpfulCount)" />
      </td>



      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="RatingCount" />
      </td>

      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="HelpfulCount" />
      </td>

      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="TotalPageViewCount" />
      </td>

      <td style="TEXT-ALIGN: center">
        <xsl:value-of select="DistinctVisitorCount" />
      </td>

      <xsl:if test="$showtopicname">
        <td style="TEXT-ALIGN: left">
          <xsl:value-of select="TopicName" />
        </td>
      </xsl:if>
    </tr>
  </xsl:template>

</xsl:stylesheet>
